#include <vector>
#include <set>
#include <iostream>
#include <unordered_map>
using namespace std;

int firstMissingPositive(vector<int>& nums) {
    int len = nums.size(); int j = 0;
    for(int i = 0; i < len; i++){
        if(nums[i] > 0 && nums[i] <= len){
            nums[j++] = nums[i];
        }
    }
    for(int i = 0; i < j; i++){
        if(nums[i]-1 < len){
            if(nums[nums[i]-1] <= len){
                nums[nums[i]-1] += len; 
            }
        }else{
            if(nums[nums[i]-1-len] <= len){
                nums[nums[i]-1-len] += len; 
            }
        }
        
    }
    int i;
    for(i = 0; i < j; i++){
        if(j == 0){
            return j+1;
        }else if(nums[i] <= len){
            return i+1;
        }
    }
    return i+1;
}

int main(){
    vector<int> nums = { 1,1000 };
    cout << firstMissingPositive(nums) << endl;
    return 0;
}